# 导入库
import numpy as np
from joblib import load

class Classification(object):
    """
    模型预测分类
    """
    def __init__(self):
        self.km = load('knn_model.joblib')
        self.scaler = load('scaler.joblib')
        self.le = load('label_encoder.joblib')

    def get_predict_category(self, X):
        """
        获取预测的分类
        """
        # 转换为预测需要的特征顺序
        features_order = ['eps', 'total_revenue_ps', 'undist_profit_ps', 'gross_margin', 'fcff', 'fcfe', 'tangible_asset', 'bps', 'grossprofit_margin', 'npta']
        new_value = np.array([X[feature] for feature in features_order])
        # 标准化缩放
        new_scaled = self.scaler.transform(new_value.reshape(1, -1))
        # 预测分类
        predicted_label = self.km.predict(new_scaled)
        predicted_category = self.le.inverse_transform(predicted_label)
        return predicted_category[0]

if __name__ == "__main__":
    cl = Classification()
    new_data = {
        'eps': 1.59,
        'total_revenue_ps': 19.4516,
        'undist_profit_ps': 7.0876,
        'fcff': 4663940800,
        'fcfe': -8729810800,
        'tangible_asset': 9468120800,
        'bps': 11.5354,
        'grossprofit_margin': 23.4565,
        'npta': 8.3336
    }
    # 预测分类
    predicted_category = cl.get_predict_category(new_data)
    print(f"预测分类: {predicted_category}")